﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Gp.Scripts.Core;
using UnityEngine;
using XNode;

namespace Gp.AI {
    
    [Serializable]
    public abstract class AIActionNode : BaseNode {
        [Input]
        [SerializeField]
        private BaseNode last;
        
        [Output(connectionType: ConnectionType.Override)]
        [SerializeField]
        private BaseNode next;
        
        
        public BaseNode Next => GetOutputPort("next")?.Connection?.node as BaseNode;
    }
    
    
    
    
    public abstract class AINodeEntry {
        public StartNode StartNode;
        public BaseUnit Unit { get; protected set; }


        private void Init(BaseUnit unit, StartNode startNode) {
            Unit = unit;
            StartNode = startNode;
            OnInit();
        }

        protected virtual void OnInit() { }


        
        
        public abstract IEnumerator Execute();

        
        
        // protected virtual IEnumerable<KeyValuePair<string, object>> Outputs() {
        //     yield break;
        // }
        //
        // protected T GetInput<T>(string name) where T : class {
        //     return _inputs.GetValueOrDefault(name) as T;
        // }
        //
        //
        // protected T GetInput<T>() where T : class {
        //     return _inputs.Values.FirstOrDefault(x => x is T) as T;
        // }

        protected IEnumerator ToNext(BaseNode node) {
            if (node == null) yield break;
            var next = node.NodeEntry();
            
            next.Init(Unit, StartNode);
            yield return next.Execute();
        }
    }


    public abstract class AINodeEntry<T> : AINodeEntry where T : BaseNode {
        protected T Data { get; }
        
        

        protected AINodeEntry(T data) {
            Data = data;
        }


    }
}